package com.fingard.dsp.bank.directbank.ceb01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * modify: 修改银行返回借贷标志
 */
@Component("CEB01Transaction")
public class CEB01Transaction extends CEB01Base implements ITransaction {

    private static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=?;";

    /**
     * 历史明细查询
     * @param p_req7516
     * @return
     * @throws Exception
     */
    //采用流处理方式
    //1、循环调用readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用writeRetDetail()，写入返回ATS的明细
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        if ("3".equals(p_req7516.savingType)) {
            return d_7516_BailAccountHisTrans_2(p_req7516);
        } else if (getBankConfig().getValueAsBool("use2ndBatchInterface")) {
            return d_7516_HisTrans_2(p_req7516);
        } else {
            return d_7156_HisTrans_1(p_req7516);
        }
    }

    private RetAtsTrans d_7156_HisTrans_1(Req7516HisTrans p_req7516) throws Exception {

        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int totalSum = 0;
                int startPatches = 1;
                boolean queryContinue = true;
                while (queryContinue) {
                    CEB01Header bankHeader = new CEB01Header();
                    bankHeader.batchID = this.getBatchID();
                    bankHeader.requestCode = "items_account";
                    bankHeader.transNodeName = "itemsPrevious";
                    bankHeader.transPatches = "1";
                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    int countRd = 1;
                    writeCommonHeaderBalTrans(xmlWriter, bankHeader);
                    xmlWriter.writeStartElement(bankHeader.transNodeName + "RequestBody");
                    xmlWriter.writeStartElement(bankHeader.transNodeName + "RequestRecord");
                    xmlWriter.writeElementString("clientPatchID", bankHeader.batchID + String.format("%04d", countRd));
                    xmlWriter.writeElementString("accountNo", p_req7516.actNum);
                    xmlWriter.writeElementString("startTime", Format.Date10Format.format(tmpCalendarFrom.getTime()) + "T00:00:00");
                    xmlWriter.writeElementString("endTime", Format.Date10Format.format(tmpClndrStepTo.getTime()) + "T23:59:59");
                    xmlWriter.writeElementString("minAmount", "");
                    xmlWriter.writeElementString("maxAmount", "");
                    xmlWriter.writeElementString("debitOrCredit", "");
                    xmlWriter.writeElementString("sortWay", "");
                    xmlWriter.writeElementString("startPatches", String.valueOf(startPatches));
                    xmlWriter.writeElementString("requestPatches", "99");
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    //测试代码：打印报文
                    //System.out.println("发送银行报文:\n"+xmlWriter.toXmlString());
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader.requestCode);
                    XmlTextReader tmpBkReader;
                    //	String[] tmpStrRet ={"",""};
                    //	tmpStrRet[0]="";
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String respondCode = tmpBkReader.getSimpleText("respondCode");
                            String respondInfo = tmpBkReader.getSimpleText("respondInfo");
                            totalSum = tmpBkReader.getSimpleTextNotNull("totalSum").length() == 0 ? 0 : Integer.parseInt(tmpBkReader.getSimpleTextNotNull("totalSum"));

                            if (respondCode != null && respondCode.equals("0000")) {
                                String ndName = bankHeader.transNodeName;
                                // #start "提取各返回节点内容"
                                while (tmpBkReader.read("/transaction/" + ndName + "/" + ndName + "Respond/" + ndName + "RespondBody/transItems")) {
                                    {
                                        //tmpRespCode = "0";
                                        tmpRespInfo = "success";
                                        RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                        XElement tmpRetNode = tmpBkReader.readedXElement;
                                        String dealDate = tmpRetNode.getSingleText("transDateTime");
                                        tmpRetDetail.actNum = p_req7516.actNum;
                                        tmpRetDetail.tranDate = dealDate.substring(0, 10);//格式2016-08-01
                                        tmpRetDetail.valueDate = tmpRetDetail.tranDate;//格式yyyy-MM-dd
                                        tmpRetDetail.tranTime = dealDate.replaceAll("T", " ");//格式"yyyy-MM-dd HH:mm:ss"
                                        // #start 提取摘要、备注，摘要和备注中有一个字段含有对账码，通过提取方法获取对账码
                                        tmpRetDetail.comment = tmpRetNode.getSingleTextNotNull("purpose").trim();
                                        tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("summary").trim();
                                        tmpRetDetail.abstr = pickupAbstr(tmpRetDetail.usage, tmpRetDetail.comment);
                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("toAccountNo").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("toAccountName").trim();
                                        tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("summary").trim();
                                        tmpRetDetail.accBal = tmpRetNode.getSingleText("accountBalance");
                                        tmpRetDetail.originator = tmpRetNode.getSingleText("sequenceNo");
                                        tmpRetDetail.bnkSeqNo = tmpRetNode.getSingleText("sequenceNo");

                                        // #end

                             /*
                              * 原有代码:
                              *  #start "发生额、余额、借贷标志 0-借 1-贷"
                                if(tmpRetNode.getSingleText("accountBalance").equals("debit")){
                                	tmpRetDetail.cdFlag ="1";
                                }else if (tmpRetNode.getSingleText("accountBalance").equals("credit")){
                                	tmpRetDetail.cdFlag ="2";
                                }
                                tmpRetDetail.cdFlag = "1";
                                #end*/

                                        String cdflag = tmpRetNode.getSingleText("debitOrCredit");
                                        if ("debit".equals(cdflag)) {
                                            tmpRetDetail.cdFlag = "1";
                                        }
                                        if ("credit".equals(cdflag)) {
                                            tmpRetDetail.cdFlag = "2";
                                        }

                                        tmpRetDetail.tranAmt = tmpRetNode.getSingleText("amount");
//                                        tmpRetDetail.originator = tmpRetNode.getSingleText("sequenceNo");
                                        retAts.writeRetDetail(tmpRetDetail);
                                    }
                                }
                                startPatches += 99;

                                if (!(startPatches < totalSum && totalSum != 0 && (totalSum > 99))) {
                                    queryContinue = false;
                                }
                            } else {
                                String returnInfo = tmpBkReader.getSimpleText("returnInfo");
                                //该期间确实没有交易记录
                                if (StringHelper.isNullOrEmpty(returnInfo) || !returnInfo.contains("已无符合条件的记录")) {
                                    tmpRespCode = "-1";
                                    respondInfo = respondInfo + " " + returnInfo;
                                }
                                tmpRespInfo = respondInfo;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 当日流水查询
     * @param p_req7515
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;//设置开始时间和结束时间都为当天
        tmpReq7516.dateTo = tmpStrToday;
        //光大银行第二批次接口查询流水接口
        if ("3".equals(p_req7515.savingType)) {
            return d_7516_BailAccountHisTrans_2(tmpReq7516);
        } else if (getBankConfig().getValueAsBool("use2ndBatchInterface")) {
            return d_7516_HisTrans_2(tmpReq7516);
        } else {
            return d_7156_HisTrans_1(tmpReq7516);
        }
    }

    /**
     * 光大银行第二批接口账户明细查询
     *
     * @param p_req7516
     * @return
     */
    private RetAtsTrans d_7516_HisTrans_2(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int totalSum = 0;
                int startPatches = 1;
                boolean queryContinue = true;
                while (queryContinue) {
                    CEB01Header bankHeader = new CEB01Header();
                    bankHeader.TransCode = "b2e004002";
                    bankHeader.batchID = this.getBatchID();
                    bankHeader.JnlDateTime = new Date();
                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    int countRd = 1;
                    writeCommonHeader_2(xmlWriter, bankHeader);
                    xmlWriter.writeStartElement("TransContent");
                    XElement tmpXElement = new XElement("ReqData", "");
                    tmpXElement.addChild(new XElement("ClientPatchID", bankHeader.batchID + String.format("%04d", countRd)));
                    tmpXElement.addChild(new XElement("AcNo", p_req7516.actNum));
                    xmlWriter.writeElementString("startTime", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("endTime", Format.Date8Format.format(tmpClndrStepTo.getTime()));
//                    tmpXElement.addChild(new XElement("startTime", p_req7516.dateFrom));
//                    tmpXElement.addChild(new XElement("endTime", p_req7516.dateTo));
                    tmpXElement.addChild(new XElement("startPatches", String.valueOf(startPatches)));
                    xmlWriter.writeXElement(tmpXElement);
                    xmlWriter.writeElementString("requestPatches", "99");
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();

                    String[] tmpStrRet = sendToBank_2(xmlWriter.toXmlString(), bankHeader.TransCode);
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpReturnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                            String respondInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                            if (tmpReturnCode != null && "0000".equals(tmpReturnCode)) {
                                totalSum = tmpBkReader.getFirstText("/Transaction/TransContent/BISHUU").length() == 0 ? 0 : Integer.parseInt(tmpBkReader.getFirstText("/Transaction/TransContent/BISHUU"));
                                while (tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")) {
                                    tmpRespCode = "0";
                                    tmpRespInfo = "success";
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    XElement tmpRetNode = tmpBkReader.readedXElement;
                                    String time = tmpRetNode.getSingleText("JIOYSJ");
                                    while (time.length() < 6) {
                                        String str = "0";
                                        str += time;
                                        time = str;
                                    }
                                    String dealDate = tmpRetNode.getSingleText("JIOYRQ") + time;
                                    dealDate = Format.DateTimeFormat.format(new SimpleDateFormat("yyyyMMddHHmmss").parse(dealDate));
                                    tmpRetDetail.actNum = tmpRetNode.getSingleTextNotNull("ZHANGH").trim();
                                    tmpRetDetail.tranDate = dealDate.substring(0, 10);//格式2016-08-01
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;//格式yyyy-MM-dd
                                    tmpRetDetail.tranTime = dealDate.replaceAll("T", " ");//格式"yyyy-MM-dd HH:mm:ss"
                                    tmpRetDetail.comment = tmpRetNode.getSingleTextNotNull("BEIZXX").trim();
                                    tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("BEIZXX").trim();
                                    if (tmpRetDetail.usage.length() == 0 || tmpRetDetail.usage == "") {
                                        tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("ZHYODM").trim();
                                    }
//                                    tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("summary").trim();
                                    tmpRetDetail.payCode = tmpRetDetail.usage;
                                    tmpRetDetail.accBal = tmpRetNode.getSingleText("ZHHUYE");
                                    tmpRetDetail.originator = tmpRetNode.getSingleText("GUIYLS");
                                    tmpRetDetail.bnkSeqNo = tmpRetNode.getSingleText("ClientPchID");
                                    tmpRetDetail.recAccNum = tmpRetNode.getSingleText("ClientPchID");
                                    //从SQLlite中取对账码
                                    Connection conn = FGBiz.getLocalDbConn();
                                    Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{tmpRetDetail.bnkSeqNo});
                                    if (selMap != null & selMap.size() > 0) {
                                        tmpRetDetail.abstr = selMap.get("BANKREQSEQID").toString();
                                    }
                                    String bkDcFlag = tmpRetNode.getSingleText("JIEDBZ");
                                    if ("1".equals(bkDcFlag)) {
                                        tmpRetDetail.cdFlag = "2";  //收入/贷
                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("DUIFZH").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("DUIFMC").trim();

                                    } else if ("0".equals(bkDcFlag)) {
                                        tmpRetDetail.cdFlag = "1";  //支出/借
                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("DUIFZH").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("DUIFMC").trim();

                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = "借贷标志有误，值：" + bkDcFlag;
                                        break;
                                    }
                                    tmpRetDetail.tranAmt = tmpRetNode.getSingleText("JIO1JE");
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                                startPatches += 99;

                                if (!(startPatches < totalSum && totalSum != 0 && (totalSum > 99))) {
                                    queryContinue = false;
                                }
                            } else {
                                String returnInfo = tmpBkReader.getFirstText("/transaction/ReturnMsg");
                                //该期间确实没有交易记录
                                if (StringHelper.isNullOrEmpty(returnInfo) || !returnInfo.contains("已无符合条件的记录")) {
                                    tmpRespCode = "-1";
                                    respondInfo = respondInfo + " " + returnInfo;
                                }
                                tmpRespInfo = respondInfo;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    private RetAtsTrans d_7516_BailAccountHisTrans_2(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int totalSum = 0;
                int startPatches = 1;
                boolean queryContinue = true;
                while (queryContinue) {
                    CEB01Header bankHeader = new CEB01Header();
                    bankHeader.TransCode = "b2e001013";
                    bankHeader.batchID = this.getBatchID();
                    bankHeader.JnlDateTime = new Date();
                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    int countRd = 1;
                    writeCommonHeader_2(xmlWriter, bankHeader);
                    xmlWriter.writeStartElement("TransContent");
                    XElement tmpXElement = new XElement("ReqData", "");
                    tmpXElement.addChild(new XElement("ClientPatchID", bankHeader.batchID + String.format("%04d", countRd)));
                    tmpXElement.addChild(new XElement("BZJAcNo", p_req7516.actNum));
                    xmlWriter.writeElementString("startTime", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("endTime", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                    tmpXElement.addChild(new XElement("startPatches", String.valueOf(startPatches)));
                    xmlWriter.writeElementString("requestPatches", "99");
                    xmlWriter.writeElementString("minAmount", "");
                    xmlWriter.writeElementString("maxAmount", "");
                    xmlWriter.writeXElement(tmpXElement);
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();

                    String[] tmpStrRet = sendToBank_2(xmlWriter.toXmlString(), bankHeader.TransCode);
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpReturnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                            String respondInfo = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                            if (tmpReturnCode != null && "0000".equals(tmpReturnCode)) {
                                totalSum = tmpBkReader.getFirstText("/Transaction/TransContent/BISHUU").length() == 0 ? 0 : Integer.parseInt(tmpBkReader.getFirstText("/Transaction/TransContent/BISHUU"));
                                while (tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")) {
                                    tmpRespCode = "0";
                                    tmpRespInfo = "success";
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    XElement tmpRetNode = tmpBkReader.readedXElement;
                                    String time = tmpRetNode.getSingleText("JIOYSJ");
                                    while (time.length() < 6) {
                                        String str = "0";
                                        str += time;
                                        time = str;
                                    }
                                    String dealDate = tmpRetNode.getSingleText("JIOYRQ") + time;
                                    dealDate = Format.DateTimeFormat.format(new SimpleDateFormat("yyyyMMddHHmmss").parse(dealDate));
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = dealDate.substring(0, 10);//格式2016-08-01
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;//格式yyyy-MM-dd
                                    tmpRetDetail.tranTime = dealDate.replaceAll("T", " ");//格式"yyyy-MM-dd HH:mm:ss"
                                    tmpRetDetail.comment = tmpRetNode.getSingleTextNotNull("ZHYODM").trim();
                                    tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("ZHYODM").trim();
                                    if (tmpRetDetail.usage.length() == 0 || tmpRetDetail.usage == "") {
                                        tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("ZHYODM").trim();
                                    }
//                                    tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("summary").trim();
                                    tmpRetDetail.payCode = tmpRetDetail.usage;
                                    tmpRetDetail.originator = tmpRetNode.getSingleText("PNGZHH");
                                    tmpRetDetail.bnkSeqNo = tmpRetNode.getSingleText("PNGZHH");
                                    tmpRetDetail.recAccNum = tmpRetNode.getSingleText("PNGZHH");
                                    //从SQLlite中取对账码
                                    Connection conn = FGBiz.getLocalDbConn();
                                    Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{tmpRetDetail.bnkSeqNo});
                                    if (selMap != null & selMap.size() > 0) {
                                        tmpRetDetail.abstr = selMap.get("BANKREQSEQID").toString();
                                    }
                                    String bkDcFlag = tmpRetNode.getSingleText("JIEDBZ");
                                    if ("1".equals(bkDcFlag)) {
                                        tmpRetDetail.cdFlag = "2";  //收入/贷
                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("DUIFZH").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("DUIFMC").trim();

                                    } else if ("0".equals(bkDcFlag)) {
                                        tmpRetDetail.cdFlag = "1";  //支出/借
                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("DUIFZH").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("DUIFMC").trim();

                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = "借贷标志有误，值：" + bkDcFlag;
                                        break;
                                    }
                                    tmpRetDetail.tranAmt = tmpRetNode.getSingleText("JIO1JE");
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                                startPatches += 99;

                                if (!(startPatches < totalSum && totalSum != 0 && (totalSum > 99))) {
                                    queryContinue = false;
                                }
                            } else {
                                String returnInfo = tmpBkReader.getFirstText("/transaction/ReturnMsg");
                                //该期间确实没有交易记录
                                if (StringHelper.isNullOrEmpty(returnInfo) || !returnInfo.contains("已无符合条件的记录")) {
                                    tmpRespCode = "-1";
                                    respondInfo = respondInfo + " " + returnInfo;
                                }
                                tmpRespInfo = respondInfo;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }
}
